﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public  class MenDian_DAL
    {
        public MenDian_DAL(Context context)
        {
            Context = context;
        }

        public Context Context { get; }
        /// <summary>
        /// 门店添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public int MDAdd(MenDian_Model m)
        {
            var m1=Context.MenDian_Model.FirstOrDefault(x=>x.MDName==m.MDName);
            if(m1!=null)
            {
                return -1; //门店已存在，请重新输入
            }
            else
            {
                Context.MenDian_Model.Add(m);
                return Context.SaveChanges();
            }
        }

        


        /// <summary>
        /// 门店列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="MDName"></param>
        /// <param name="MDZT"></param>
        /// <param name="Count"></param>
        /// <param name="Count2"></param>
        /// <returns></returns>
        public List<MenDian_Model> MDShow(int index, int size, string? MDName, int? MDZT, out int Count, out int Count2)
        {
            var m = Context.MenDian_Model.AsQueryable().ToList();
            if (!string.IsNullOrEmpty(MDName))
            {
                m = m.Where(x => x.MDName.Contains(MDName)).ToList();
            }
            if (MDZT!=null)
            {
                m=m.Where(x=>x.MDZT==MDZT).ToList();
            }
            Count = m.Count();
            Count2 = (int)Math.Ceiling(Count * 1.0 / size);
            return m.OrderBy(x=>x.MDId).Skip((index-1)*size).Take(size).ToList();
        }
        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="MDid"></param>
        /// <param name="MDZT"></param>
        /// <returns></returns>
        public int MDXZT(int MDid,int MDZT)
        {
            var m=Context.MenDian_Model.FirstOrDefault(x=>x.MDId==MDid);
            if (MDZT==0)
            {
                m.MDZT = 0;
            }
            else
            {
                m.MDZT = 1;
            }
            return Context.SaveChanges();
        }
        /// <summary>
        /// 门店反填
        /// </summary>
        /// <param name="MDid"></param>
        /// <returns></returns>
        public MenDian_Model MDFan(int MDid)
        {
           var m= Context.MenDian_Model.FirstOrDefault(x=>x.MDId==MDid);
            return m;
        }

        /// <summary>
        /// 门店修改
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public int MDXiouGai(MenDian_Model m)
        {
            
            var m1=Context.MenDian_Model.FirstOrDefault(x=>x.MDId == m.MDId);
            Context.MenDian_Model.Update(m1);
            m1 = m;
            return Context.SaveChanges();   
        }

    }
}
